#pragma once
#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
namespace myaddr
{
    class InetAddr
    {
    public:
        InetAddr(struct sockaddr_in addr) : _addr(addr)
        {
            _ip = inet_ntoa(_addr.sin_addr);
            _port = ntohs(_addr.sin_port);
        }
        InetAddr(const std::string &ip, uint16_t port) : _ip(ip), _port(port)
        {
            _addr.sin_family = AF_INET;
            inet_aton(_ip.c_str(), &_addr.sin_addr);
            _addr.sin_port = htons(_port);
        }
        ~InetAddr() {}
        std::string GetString() const
        {
            return _ip + ":" + std::to_string(_port);
        }
        std::string Ip() const { return _ip; }
        uint16_t Port() const { return _port; }
        struct sockaddr *GetInet() const
        {
            return (struct sockaddr *)&_addr;
        }
        socklen_t Len() const
        {
            return sizeof(_addr);
        }
        bool operator==(const InetAddr &addr) const
        {
            return addr._ip == _ip && addr._port == _port;
        }

    private:
        struct sockaddr_in _addr; // 网络
        std::string _ip;  //本机
        uint16_t _port;  //本机
    };
}